<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${!dataId ? '新增' : '编辑'}-岗位`"
		width="500px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		append-to-body
		:show-update-audit-btn="false"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" :show-message="false" label-width="100px" size="medium">
			<el-row>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="编号" prop="code">
						<el-input v-model="ruleForm.code" placeholder="编号" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="名称" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="岗位分类" prop="typeId">
						<el-select v-model="ruleForm.typeId" class="width-100-percent" placeholder="岗位分类">
							<el-option
								v-for="item in postTypeList"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="是否生产岗" prop="ifMainPost">
						<el-radio-group v-model="ruleForm.ifMainPost" @change="onMainPostChange">
							<el-radio-button :label="true">是</el-radio-button>
							<el-radio-button :label="false">否</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col v-if="ruleForm.ifMainPost" :span="22">
					<el-form-item class="margin-bottom-10" label="工序">
						<jk-select v-model="ruleForm.processId" size="medium" class="width-100-percent" placeholder="工序" :option-list="processList"></jk-select>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="排序" prop="sortNum">
						<el-input-number v-model="ruleForm.sortNum" class="width-100-percent" controls-position="right" :min="0" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item label="备注">
						<el-input v-model="ruleForm.remark" type="textarea" placeholder="" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>

</template>
<script>
    import { systemPostCreate, systemPostUpdate, systemPostGet } from '../../../api/system/post';
    import { systemBusinessDictPage } from '../../../api/basicData/businessDict';

    export default {
        props: {
            // 页面权限的前缀
            permissionPrefix: {
                type: String,
                default: ''
            },
            dataId: {
                type: [String, Number],
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                processList: [],
                showLoading: false,
                postTypeList: [],
                saveBtnLoading: false,
                showDialog: false,
                ruleForm: {
                    'code': '',
                    'auditState': 1,
                    'ifMainPost': true,
                    'ifRegularDaily': true,
                    'ifWatcher': true,
                    'name': '',
                    'processCode': '',
                    'processId': null,
                    'processName': '',
                    'sortNum': 0,
                    'typeId': null,
                    'typeName': '',
                    'wageType': 0
                },
                rules: {
                    typeId: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    ifMainPost: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            onMainPostChange(e) {
                if (!e) {
                    this.$set(this.ruleForm, 'processId', null);
                    this.$set(this.ruleForm, 'processCode', null);
                    this.$set(this.ruleForm, 'processName', null);
                }
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                params.typeName = this.postTypeList.find(x => x.id === params.typeId).name;
                const processItem = this.processList.find(x => x.id === params.processId) || {};
                params.processCode = processItem.code || '';
                params.processName = processItem.name || '';
                return params;
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                if (!this.dataId) {
                    this.ruleForm.id = null;
                    systemPostCreate(this.getParamData()).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$emit('on-finish');
                            this.$tipsBar(this, 'save');
                        }
                    });
                } else {
                    systemPostUpdate(this.getParamData()).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$emit('on-finish');
                            this.$tipsBar(this, 'save');
                        }
                    });
                }
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    }
                });
            },
            // 取消按钮
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return systemPostGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
            getPostTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'POST_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.postTypeList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                this.processList = this.$store.state.user.userProcess.processList;
                if (!this.dataId) {
                    this.$set(this.ruleForm, 'processId', this.$store.state.user.userProcess.defaultProcessId);
                    this.$set(this.ruleForm, 'processCode', this.$store.state.user.userProcess.defaultProcessCode);
                    this.$set(this.ruleForm, 'processName', this.$store.state.user.userProcess.defaultProcessName);
                }
                await this.getPostTypeListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
